#include "UpperSearchResponseCmd.h"
#include "Main/GateWay.h"
UpperSearchResponseCmd::UpperSearchResponseCmd(QObject *parent) :
    BaseCmd(parent)
{
    this->m_protocolType = ProtocolType::CloudProtocol;
    this->m_functionCode = FunctionCode;
    this->m_deviceType = 0;
    this->m_mode = 0;
}

Package UpperSearchResponseCmd::toPackage()
{
    Package package;
    QByteArray body;
    quint16 strLength;
    System *system = System::getInstance();
    Zigbee3ListXml *zigbee3listxml=Zigbee3ListXml::getInstance();
    QString appversion = system->appVersion();
    QString systemversion=system->systemVersion();
    quint16 versionlength=0;
    QByteArray ZigbeeVersion=zigbee3listxml->ZigbeeVersion();
    int dateversion=(ZigbeeVersion.toHex().toInt(0,10));
    QByteArray Version=QByteArray::number(dateversion);
    qDebug()<<"version="<<dateversion;
    QByteArray versiondata=(appversion+"/").toUtf8();
    versiondata.append(Version);
    qDebug()<<"versiondata="<<versiondata;
//    versiondata=versiondata+"/"+zigbee3listxml->ZigbeeVersion();
    versionlength=versiondata.size();
    body.append(DataTypeConver::intToByteArray(m_deviceType,1));

    strLength = m_mac.size();
    body.append(DataTypeConver::intToByteArray(strLength,2));
    body.append(m_mac);

    strLength = m_registrationDateBCD.size();
    body.append(DataTypeConver::intToByteArray(strLength,2));
    body.append(m_registrationDateBCD);

    strLength = m_registrant.size();
    body.append(DataTypeConver::intToByteArray(strLength,2));
    body.append(m_registrant);

    body.append(DataTypeConver::intToByteArray(m_mode,1));

    strLength = m_ip.size();
    body.append(DataTypeConver::intToByteArray(strLength,2));
    body.append(m_ip);

    strLength = m_mask.size();
    body.append(DataTypeConver::intToByteArray(strLength,2));
    body.append(m_mask);

    strLength = m_gateway.size();
    body.append(DataTypeConver::intToByteArray(strLength,2));
    body.append(m_gateway);
    body.append(DataTypeConver::intToByteArray(versionlength,2));
    body.append(versiondata);
    body.append(DataTypeConver::intToByteArray(systemversion.size(),2));
    body.append(systemversion);
    body.append(DataTypeConver::intToByteArray(m_KNXMac,4));
    body.append(DataTypeConver::intToByteArray(m_KNXStatus,1));
    body = encrypt(body,KeyType::AES);

    package.setId(m_cmdId);
    package.setFunction(FunctionCode);
    package.setBody(body);

    return package;
}

void UpperSearchResponseCmd::unPackage(Package package)
{
    m_cmdId = package.id();
}

quint8 UpperSearchResponseCmd::deviceType() const
{
    return m_deviceType;
}

void UpperSearchResponseCmd::setDeviceType(const quint8 &deviceType)
{
    m_deviceType = deviceType;
}

QString UpperSearchResponseCmd::mac() const
{
    return m_mac;
}

void UpperSearchResponseCmd::setMac(const QString &mac)
{
    m_mac = mac;
}

QString UpperSearchResponseCmd::registrant() const
{
    return m_registrant;
}

void UpperSearchResponseCmd::setRegistrant(const QString &registrant)
{
    m_registrant = registrant;
}

QByteArray UpperSearchResponseCmd::registrationDateBCD() const
{
    return m_registrationDateBCD;
}

void UpperSearchResponseCmd::setRegistrationDateBCD(const QByteArray &registrationDateBCD)
{
    m_registrationDateBCD = registrationDateBCD;
}

quint8 UpperSearchResponseCmd::mode() const
{
    return m_mode;
}

void UpperSearchResponseCmd::setMode(const quint8 &mode)
{
    m_mode = mode;
}

QString UpperSearchResponseCmd::ip() const
{
    return m_ip;
}

void UpperSearchResponseCmd::setIp(const QString &ip)
{
    m_ip = ip;
}

QString UpperSearchResponseCmd::mask() const
{
    return m_mask;
}

void UpperSearchResponseCmd::setMask(const QString &mask)
{
    m_mask = mask;
}

QString UpperSearchResponseCmd::gateway() const
{
    return m_gateway;
}

void UpperSearchResponseCmd::setGateway(const QString &gateway)
{
    m_gateway = gateway;
}

quint32 UpperSearchResponseCmd::getKNXMac() const
{
    return m_KNXMac;
}
void UpperSearchResponseCmd::setKNXMac(const quint32 mac)
{
    m_KNXMac = mac;
}
quint8 UpperSearchResponseCmd::getKNXStatus() const
{
    return m_KNXStatus;
}
void UpperSearchResponseCmd::setKNXStatus(const quint8 status)
{
    m_KNXStatus = status;
}

